package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.exception.CommonException;
import com.cjack.frenchexcel.model.AdminUserModel;
import com.cjack.frenchexcel.model.CooperativeNumberModel;
import com.cjack.frenchexcel.service.CooperativeNumberService;
import com.cjack.frenchexcel.service.UserService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 合作商编号管理
 */
@RestController
@RequestMapping("/cooperativeNumber")
public class CooperativeNumberEndpoint extends BaseEndpoint{

    @Autowired
    CooperativeNumberService service;
    @Autowired
    UserService userService;

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list/", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult list( CooperativeNumberModel condition) {

        try{
            //注意pageDate里面的页数是从0开始计算的
            List<CooperativeNumberModel> datas = service.list( condition);
            for( CooperativeNumberModel data : datas){

            }
            //排序，编号为空的排在前面
            Collections.sort(datas, (o1, o2) -> {
                if( StringUtils.isEmpty( o1.getNumber())){
                    return 1;
                }
                return 0;
            });
            return AjaxResult.SUCCESS( datas);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 查询供应商列表-ALL
     * @return
     */
    @RequestMapping(value = "/listForSelect", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult listForSelect(@RequestParam(required = false) String jobNumber) {
        try{
            //注意pageDate里面的页数是从0开始计算的
            List<CooperativeNumberModel> datas = null;
            if (org.apache.commons.lang3.StringUtils.isNotBlank(jobNumber)) {
                datas = service.listForSelectByJobNumber(jobNumber);
            } else {
                datas = service.listForSelect();
            }
            return AjaxResult.SUCCESS( datas);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 查询供应商列表-ALL
     * @return
     */
    @RequestMapping(value = "/listForSelectByUserId", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult listForSelectByUserId(@RequestParam(required = false) Long userId) {
        try{
            //注意pageDate里面的页数是从0开始计算的
            List<CooperativeNumberModel> datas = Lists.newArrayList();
            AdminUserModel userInfo = null;
            if (userId != null) {
                userInfo = userService.getUserInfo(userId);
            }
            String jobNumber = null;
            if (userInfo != null) {
                jobNumber = userInfo.getJobNumber();
            }
            if (org.apache.commons.lang3.StringUtils.isNotBlank(jobNumber)) {
                datas = service.listForSelectByJobNumber(jobNumber);
            } else {
                datas = service.listForSelect();
            }

            datas = datas.stream().filter(item -> Objects.nonNull(item.getName())).sorted(Comparator.comparing(CooperativeNumberModel::getName)).collect(Collectors.toList());
            return AjaxResult.SUCCESS( datas);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 查询供应商列表 BY job_number
     * @return
     */
    @RequestMapping(value = "/listForSelect/jobNumber/{jobNumber}", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult listForSelectByConsumerRecordId(@PathVariable String jobNumber) {
        try{
            List<CooperativeNumberModel> numberModels = service.listForSelectByJobNumber(jobNumber);
            return AjaxResult.SUCCESS(numberModels);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 新增
     * @return
     */
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addOrUpdate( CooperativeNumberModel model) {
        try{
            if( isAdd( model.getId())){
                model.setCreateTime( new Date());
            }
            service.save( model);
            return AjaxResult.SUCCESS();
        }catch ( CommonException e) {
            e.printStackTrace();
            return AjaxResult.ERROR( e.getCode(), e.getMessage());
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult del( CooperativeNumberModel model) {
        try{
            service.del( model);
            return AjaxResult.SUCCESS( "删除成功");
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
